package cuongnh.project.japanesediary.vocabulary.ui.editor.vocabulary;

import java.util.Iterator;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.IFormPage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import cuongnh.project.japanesediary.vocabulary.common.CommonServices;
import cuongnh.project.japanesediary.vocabulary.model.Vocabulary;
import cuongnh.project.japanesediary.vocabulary.ui.editor.vocabulary.page.category.VocabularyCategoryMDPage;
import cuongnh.project.japanesediary.vocabulary.ui.editor.vocabulary.page.example.VocabularyExamplesPage;
import cuongnh.project.japanesediary.vocabulary.ui.editor.vocabulary.page.general.VocabularyGeneralInfoPage;
import cuongnh.project.japanesediary.vocabulary.ui.editor.vocabulary.page.grammar.VocabularyGrammarsPage;
import cuongnh.project.japanesediary.vocabulary.ui.editor.vocabulary.page.related.RelatedVocabularyPage;

public class VocabularyEditor extends FormEditor implements
		IWorkbenchWindowActionDelegate {
	
	private static Logger logger = LoggerFactory.getLogger(VocabularyEditor.class);

	public static final String ID = "cuongnh.project.japanesediary.vocabulary.ui.editor.vocabulary.VocabularyEditor"; //$NON-NLS-1$

	private IWorkbenchWindow window;

	@Autowired
	private CommonServices commonServices;

	private Vocabulary vocabulary;

	@Override
	protected void addPages() {
		try {
			addPage(new VocabularyGeneralInfoPage(this, vocabulary,
					commonServices, "1", "General information"));
			addPage(new VocabularyExamplesPage(this, "2", "Examples", vocabulary,
					commonServices));
			addPage(new VocabularyGrammarsPage(this, "3", "Related Grammars", vocabulary,
					commonServices));
			addPage(new VocabularyCategoryMDPage(this, "4", 
					commonServices));
			addPage(new RelatedVocabularyPage(this, vocabulary,
					commonServices, "5",
					"Related vocabularies"));			
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		for (Iterator<?> iter = this.pages.iterator(); iter.hasNext();) {
			Object obj = iter.next();
			if (obj instanceof IFormPage) {
				((IFormPage) obj).doSave(null);
			}
		}

		// Update dirty state changed
		this.editorDirtyStateChanged();
	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void run(IAction action) {
		IWorkbenchPage page = window.getActivePage();
		try {
			page.openEditor(new VocabularyEditorInput(1), this.ID);
		} catch (PartInitException e) {
			System.out.println(e);
		}

	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(IWorkbenchWindow window) {
		this.window = window;

	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		super.init(site, input);
		if (input instanceof VocabularyEditorInput) {
			this.vocabulary = commonServices.getVocabularyDao().get(
					((VocabularyEditorInput) input).getId());
			setPartName("V:" + vocabulary.getVocabulary());
		}
	}

	@Override
	protected void setPartName(String partName) {
		super.setPartName(partName);
	}

}
